const VisitorInfo = require("./schema/visitorInfoSchema");

// 查询全局设置
module.exports.findvisitorInfoDao = async function (pageInfo) {
  const total = await VisitorInfo.countDocuments();
   const rows = await VisitorInfo.find()
    .sort({ "adInfoOb.lastTime": -1 })
    .skip((pageInfo.page - 1) * pageInfo.limit)
    .limit(pageInfo.limit)

  return {
    total,
    rows,
  };
};
//添加访客记录
module.exports.visitorInfoSittingDao = async function (ip, adInfoOb) {
  const result = await VisitorInfo.findOne({ ip: ip });
  if (!result) {
    await VisitorInfo.create({
      adInfoOb,
      ip,
    });
  } else {
    await VisitorInfo.updateOne(
      { ip: ip },
      {
        adInfoOb: {
          ...adInfoOb,
          totNum: result.adInfoOb.totNum + 1,
          lastTime: Date.now(),
        },
      }
    );
  }
};
